// pages/userCenter/myCarinfo/myCarinfo.js
const request = require('../../../utils/request')

Page({
  data: {
    insuranceTime: "",
    lastTime: "",
    checkTime: "",
    licenceNum: "",
    endTime: new Date().getTime(),
    date: '',
  },

  onLoad() {
    this.getMerberInfo()
  },

  getMerberInfo() {
    wx.showLoading()
    request.get('member_info')
      .then(res => {
        this.setData({
          insuranceTime: res.data.baoxian,
          lastTime: res.data.last_baoyang_time,
          checkTime: res.data.last_jiancha_time,
          licenceNum: res.data.chepai,
        })
      })
      .catch(error => {
        wx.showToast({
          icon: 'none',
          title: error.msg && error.msg
        })
      })
      .finally(_ => {
        wx.hideLoading()
      })
  },
  
  onInsuranceChange(e) {
    this.setData({
      insuranceTime: e.detail.value
    })
  },
  
  onCheckChange(e) {
    this.setData({
      checkTime: e.detail.value
    })
  },

  onLastChange(e) {
    this.setData({
      lastTime: e.detail.value
    })
  },

  onNumChange(e) {
    this.setData({
      licenceNum: e.detail
    })
  },
  
  submit(){
    if (!this.data.insuranceTime) {
      wx.showToast({
        icon: 'none',
        title: '请选择上保险时间'
      })
      return
    }

    if (!this.data.lastTime) {
      wx.showToast({
        icon: 'none',
        title: '请选择上次保养时间'
      })
      return
    }

    if (!this.data.checkTime) {
      wx.showToast({
        icon: 'none',
        title: '请选择上次检查时间'
      })
      return
    }

    if (!this.data.licenceNum) {
      wx.showToast({
        icon: 'none',
        title: '请输入车牌号'
      })
      return
    }
    
    const data = {
      baoxian: this.data.insuranceTime,
      last_baoyang_time: this.data.lastTime,
      last_jiancha_time: this.data.checkTime,
      chepai: this.data.licenceNum
    }
    console.log(data)
    wx.showLoading()
    request.post('info_update', data)    
      .then(res => {
        wx.showToast({
          icon: 'none',
          title: res.msg
        })
      })
      .catch(error => {
        wx.showToast({
          icon: 'none',
          title: error.msg && error.msg
        })
      })
      .finally(_ => {
        wx.hideLoading()
      })
  }
})